package com.anlu.concurrent.ch5;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2021/10/27 16:52
 */
public class TestMap {
    public static void main(String[] args) {
        int initialCapacity = 16;
        float loadFactor=0.75f;
        int concurrencyLevel =16;

        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0){
            throw new IllegalArgumentException();
        }

        if (concurrencyLevel > 65536){
            concurrencyLevel = 65536;
        }
        int sshift = 0;
        int ssize = 1;
        while (ssize < concurrencyLevel) {
            ++sshift;
            ssize <<= 1;
        }

        int segmentShift = 32 - sshift;
        int segmentMask = ssize - 1;
        if (initialCapacity > 1073741824) {
            initialCapacity = 1073741824;
        }
        int c = initialCapacity / ssize;
        if (c * ssize < initialCapacity) {
            ++c;
        }
        int cap = 2;
        while (cap < c) {
            cap <<= 1;
        }
        System.out.println(cap * loadFactor);

        System.out.println("the cap is : " + cap);
        System.out.println("the resize cap is : " +cap * loadFactor);
        int hash = hash(123456);
        System.out.println("the hash is : " + Integer.toBinaryString(hash));
        System.out.println("the hash >>> segmentShift is : "+ segmentShift+":"
                + Integer.toBinaryString(hash >>> segmentShift));
        System.out.println("the segmentMask is : " +segmentMask+":"
                + Integer.toBinaryString(segmentMask));
        int j = (hash >>> segmentShift) & segmentMask;

    }
    private static int hash(Object k) {

        int h = 13;
        h ^= k.hashCode();

        h += (h <<  15) ^ 0xffffcd7d;
        h ^= (h >>> 10);
        h += (h <<   3);
        h ^= (h >>>  6);
        h += (h <<   2) + (h << 14);
        return h ^ (h >>> 16);
    }
}
